//
//  SettingsViewController.m
//  Comma
//
//  Created by dlios on 15-1-13.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SettingsViewController.h"
#import "SettingsView.h"
#import "PasswordChangeViewController.h"
#import "LoginAndRegisterViewController.h"
#import "SDImageCache.h"
#import "YRSideViewController.h"
#import "SSKeychain.h"
#import "AboutCommaViewController.h"
@interface SettingsViewController ()

@end

@implementation SettingsViewController
- (void)loadView
{
    [super loadView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.settings = [[SettingsView alloc] initWithLeftButton:@"" RightButton:@"关于" Title:@"设置" FooterView:FooterViewStateNone];
    [self.settings createMySettings];
    self.view = self.settings;
    
#pragma mark - 修改密码
#pragma mark -  这两个都是button   一个是button  一个是后面的大button
    self.password = self.settings.changePassword;
    self.passwordView = self.settings.changeButton;
    
    
    [self.password addTarget:self action:@selector(changePasswordAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.passwordView addTarget:self action:@selector(changePasswordAction:) forControlEvents:UIControlEventTouchUpInside];
    
#pragma mark - 清空缓存
    
    [self.settings.clearBackButton addTarget:self action:@selector(clearAllMemoryAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.settings.clearButton addTarget:self action:@selector(clearAllMemoryAction:) forControlEvents:UIControlEventTouchUpInside];
    
  
#pragma mark - 注销登录
    
    [self.settings.logoutButton addTarget:self action:@selector(logoutButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
#pragma mark - 抽屉效果返回
    
    [self.settings.leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
#pragma mark - 关于
    
    [self.settings.rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)leftButtonAction:(id)sender
{
    YRSideViewController * sideview = [YRSideViewController share];
    sideview.needSwipeShowMenu= YES;
    if (sideview.rootViewController.view.frame.origin.x == 0) {
        [sideview showLeftViewController:YES];
        [sideview.rootViewController.view setFrame:CGRectMake(SCREEN_WIDTH / 3 * 2, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    }
    else
    {
        [sideview.rootViewController.view setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    }

}

- (void)rightButtonAction:(id)sender
{
    AboutCommaViewController *about = [[AboutCommaViewController alloc] init];
    [self.navigationController pushViewController:about animated:YES];
}
- (void)changePasswordAction:(id)sender
{
    PasswordChangeViewController *password =[[PasswordChangeViewController alloc] init];
    [self.navigationController pushViewController:password animated:YES];
}
#pragma mark - 注销的点击事件
- (void)logoutButtonAction:(id)sender
{

    if (IOS_VERSION >= 8.0) {
        UIAlertController *logoutAlert = [UIAlertController alertControllerWithTitle:@"您确定要注销吗" message:@"注销后需要重新登录哦~" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            NSLog(@"111");

        }];
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//            点击确定后 跳转主界面登录界面 跳转登路界面需要注销用户信息吗？？
//            需要测试
            NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
            [def setObject:@"nil" forKey:@"login"];
            //删除本地存储的钥匙串
            [SSKeychain deletePasswordForService:@"com.isuso" account:@"uuid"];
            [SSKeychain deletePasswordForService:@"com.isuso" account:@"appriveKey"];
            LoginAndRegisterViewController *login = [[LoginAndRegisterViewController alloc] init];
            UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:login];
            [self presentViewController:nav animated:YES completion:nil];
            
            nav.navigationBarHidden = YES;
        }];
        [logoutAlert addAction:cancel];
        [logoutAlert addAction:confirm];
        
        [self presentViewController:logoutAlert animated:YES completion:nil];
    }
    else
    {
        UIAlertView *logoutAlert = [[UIAlertView alloc] initWithTitle:@"您确定要注销吗" message:@"注销后需要重新登录哦~" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [logoutAlert show];
    }
}
#pragma mark - 清除缓存的点击事件
- (void)clearAllMemoryAction:(id)sender
{
    
   
    
    if (IOS_VERSION >= 8.0) {
        
        UIAlertController *clearAlert = [UIAlertController alertControllerWithTitle:nil message:@"缓存已经清理" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:clearAlert animated:YES completion:nil];
        [self performSelector:@selector(dismissMyAlertView) withObject:clearAlert afterDelay:1.2];
        //显示归零
        
    }
    else
    {
        UIAlertView *clearAlert = [[UIAlertView alloc] initWithTitle:nil message:@"缓存已经清理" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [clearAlert show];
        [self performSelector:@selector(dismissAlert:) withObject:clearAlert afterDelay:1.2];
    }
    
}
#pragma mark - 关于清除缓存 还存在问题
- (void)dismissMyAlertView
{
    [self.settings.sdImage clearMemory];
    [self.settings.sdImage clearDisk];
    [self dismissViewControllerAnimated:YES completion:nil];
    float memory = [self.settings.sdImage checkTmpSize];
    NSString *myMemory = [NSString stringWithFormat:@"%.1f M",memory];
    
    NSLog(@"清理了内存：%@",myMemory);
    [self.settings.memorySize setText:@"0.0 M"];
}

- (void)dismissAlert:(UIAlertView *)alert
{
    if(alert){
        [alert dismissWithClickedButtonIndex:[alert cancelButtonIndex] animated:YES];
        
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {

        NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
        [def setObject:@"nil" forKey:@"login"];
        //删除本地存储的钥匙串
        [SSKeychain deletePasswordForService:@"com.isuso" account:@"uuid"];
        [SSKeychain deletePasswordForService:@"com.isuso" account:@"appriveKey"];
        LoginAndRegisterViewController *login = [[LoginAndRegisterViewController alloc] init];
        UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:login];
        [self presentViewController:nav animated:YES completion:nil];
        
        nav.navigationBarHidden = YES;

    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
